#include "stdafx.h"
#include "ckEnumDataSource.h"
#include "ckDataSource.h"

namespace ck
{
	EnumDataSource::EnumDataSource()
	{
	}


	EnumDataSource::~EnumDataSource()
	{
		Cleanup();
	}

	void EnumDataSource::Add(DataSource* pSource)
	{
		if (pSource)
		{
			m_sources.push_back(pSource);
		}
	}

	void EnumDataSource::Cleanup()
	{
		std::vector<DataSource*>::iterator iter;
		for (iter = m_sources.begin(); iter != m_sources.end(); iter++)
		{
			(*iter)->Release();
		}
		m_sources.clear();
	}

	void EnumDataSource::Reset()
	{
		m_iter = m_sources.begin();
	}

	DataSource* EnumDataSource::Next()
	{
		if (m_iter == m_sources.end())
		{
			return NULL;
		}
		return *(m_iter++);
	}

	DataSource*	EnumDataSource::Find(const char* szName)
	{
		std::vector<DataSource*>::iterator	iter;
		for (iter = m_sources.begin(); iter != m_sources.end(); iter++)
		{
			if (strcmp((*iter)->GetName(), szName) == 0)
			{
				return *iter;
			}
		}
		return NULL;
	}

	RESULTCODE EnumDataSource::Remove(const char* szName)
	{
		if (szName == NULL)
		{
			return RC_FAILURE;
		}

		std::vector<DataSource*>::iterator	iter;
		for (iter = m_sources.begin(); iter != m_sources.end(); iter++)
		{
			if (strcmp((*iter)->GetName(), szName) == 0)
			{
				DataSource* pDataSource = *iter;
				pDataSource->Close();
				pDataSource->Release();

				m_sources.erase(iter);

				return RC_SUCCESS;
			}
		}

		return RC_FAILURE;;
	}
}

